import axios from 'axios'
import qs from "qs"
import {baseURL} from "@/config/baseConfig";
import localstorage from '@/tools/localstorage';

const baseRequest = axios.create({
    baseURL,
    timeout: 5000,
    headers: {
        "Access-Control-Allow-Origin": "*",
        "Authorization" : localstorage.getItemObject("user")?.Password
    },
    // 跨域请求时携带 session
    withCredentials: true
})

export default axios.create({
        baseURL
    }
)

export function GET(url: string, config: object = {}) {
    return baseRequest.get(url, config);
}

export function GETA(url: string, config: object = {}) {
    return baseRequest.get(url, {
        headers:{
            "Authorization" : localstorage.getItemObject("user")?.Password
        }
    });
}

export function POST_QS(url: string, data: object = {}, config: object = {}) {
    return baseRequest.post(url, qs.stringify(data), config);
}

export function POST(url: string, data: any = {}, config: object = {}) {
    return baseRequest.post(url, data, config);
}

export function PUT(url: string, data: object = {}, config: object = {}) {
    return baseRequest.put(url, data, config)
}

export function PUT_QS(url: string, data: object = {}, config: object = {}) {
    return baseRequest.put(url, qs.stringify(data), config)
}

export function DELETE(url: string, config: object = {}) {
    return baseRequest.delete(url, config);
}
